<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="700px">
			<el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="插件名称">
							<el-input v-model="state.ruleForm.cname" placeholder="请输入插件名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="插件功能">
							<el-select v-model="state.ruleForm.action_type" placeholder="请选择插件功能" size="default" class="w100">
								<el-option label="替换" value="redirect"></el-option>
								<el-option label="屏蔽" value="block"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-if="state.ruleForm.action_type ==='redirect'">
						<el-form-item label="替换链接">
							<el-input v-model="state.ruleForm.redirect" placeholder="请输入替换链接" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="条件">
							<template v-if="state.ruleForm.condition.length" >
								<div class="w100 shangxian" style="display:flex;" v-for="(item,index) in state.ruleForm.condition" :key="index">
									<div style="width: calc(100% - 70px);display:flex;align-items:center">
										<el-input v-model="item.name" placeholder="参数名称" style="width: 180px;margin-right: 5px;"></el-input>
										<el-select v-model="item.type" placeholder="参数类型" size="default" style="width: 150px;margin-right: 5px;">
											<el-option label="数组" value="array"></el-option>
											<el-option label="字符串" value="string"></el-option>
										</el-select>
										<el-input v-model="item.value" placeholder="参数值(数组请用|分隔)"></el-input>
									</div>
									<div style="width: 70px;display:flex">
										<div class="addSum" style="background-color: dodgerblue;" @click="addUpper" v-if="index == state.ruleForm.condition.length - 1">+</div>
										<div class="addSum" style="background-color: red;line-height: 26px;" @click="sumUpper(index)">-</div>
									</div>
								</div>
							</template>
							<div style="width: 70px;display:flex" v-else>
								<div class="addSum" style="background-color: dodgerblue;" @click="addUpper">+</div>
							</div>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="插件状态">
							<el-switch v-model="state.ruleForm.action" active-value="1" inactive-value="0" inline-prompt
								active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemDicDialog">
import { reactive, ref,defineAsyncComponent } from 'vue';
import { systemPluginApi } from '/@/api/system/plugin';
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		cname: '',
		action_type:'',
		redirect:'',
		action:"1",
		condition:[
			{
				name:'urlFilter',
				type:'string',
				value:'',
			},
			{
				name:'resourceTypes',
				type:'array',
				value:'',
			}
		]
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const addUpper = ()=>{
	state.ruleForm.condition.push({
		name:'',
		type:'string',
		value:'',
	})
}
const sumUpper = (index)=>{
	state.ruleForm.condition.splice(index,1);
}
// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.ruleForm.condition = JSON.parse(state.ruleForm.condition);
		state.dialog.title = '修改插件';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增插件';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			cname: '',
			action_type:'',
			redirect:'',
			action:"1",
			condition:[
				{
					name:'urlFilter',
					type:'string',
					value:'',
				},
				{
					name:'resourceTypes',
					type:'array',
					value:'',
				}
			]
		}
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		let ruleForm = JSON.parse(JSON.stringify(state.ruleForm));
		ruleForm.condition = JSON.stringify(state.ruleForm.condition)
		systemPluginApi().add(ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}else{
		let ruleForm = JSON.parse(JSON.stringify(state.ruleForm));
		ruleForm.condition = JSON.stringify(state.ruleForm.condition)
		systemPluginApi().edit(ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
.addSum{
	width: 30px;
	height: 30px;
	color: #fff;
	border-radius: 50%;
	line-height: 30px;
	text-align: center;
	font-size: 20px;
	margin-left:5px;
	cursor: pointer;
}
.shangxian{
	margin-bottom: 10px;
	&:last-child{
		margin-bottom: 0;
	}
}
</style>
